﻿using System.Linq;
using System.Net;
using System.Web.Mvc;
using TestApplication.Domain.Models;
using TestApplication.WebUI.Areas.Manage.Models;

namespace TestApplication.WebUI.Areas.Manage.Controllers
{
    public class ProductsController : Controller
    {
        private readonly UnitOfWork _unitOfWork;

        public ProductsController(UnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View(_unitOfWork.ProductRepository.AsQueryable.ToList());
        }

        public ActionResult Create()
        {
            var storeList = _unitOfWork.StoreRepository
                                       .AsQueryable
                                       .Select(it => new { Value = it.Id, Text = it.Title });
            var selectList = new SelectList(storeList, "Value", "Text");

            return View(new ProductCreationForm { StoreList = selectList });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "StoreId,Name,Description")] Product product)
        {
            if (!ModelState.IsValid)
            {
                return View(product);
            }

            _unitOfWork.ProductRepository.Add(product);
            _unitOfWork.SaveChanges();
            return RedirectToAction("Details", new { id = product.Id });
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var product = _unitOfWork.ProductRepository.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            return View(product);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var product = _unitOfWork.ProductRepository.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            return View(product);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Description")] Product product)
        {
            if (!ModelState.IsValid)
            {
                return View(product);
            }

            _unitOfWork.ProductRepository.Add(product);
            _unitOfWork.SaveChanges();
            return RedirectToAction("Details", new { id = product.Id });
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var product = _unitOfWork.ProductRepository.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }

            return View(product);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            _unitOfWork.ProductRepository.Remove(id);
            _unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}